package com.lc.ibps.platform.rest.org;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lc.ibps.api.base.model.User;
import com.lc.ibps.api.org.constant.PartyType;
import com.lc.ibps.api.org.service.IPartyUserService;
import com.lc.ibps.base.core.util.AppUtil;
import com.lc.ibps.base.core.util.JacksonUtil;
import com.lc.ibps.base.core.util.string.StringUtil;
import com.lc.ibps.base.web.context.ContextUtil;
import com.lc.ibps.cloud.entity.APIResult;
import com.lc.ibps.org.party.persistence.entity.DefaultPartyUserPo;
import com.lc.ibps.web.controller.BaseApiController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 用户服务 控制器，第三方系统调用。
 *
 * <pre>
 *  
 * 构建组：ibps-platform-webapi
 * 作者：eddy
 * 邮箱：1546077710@qq.com
 * 日期：2016年9月22日-下午2:47:07
 * 版权：广州流辰信息技术有限公司版权所有
 * </pre>
 */

@RequestMapping(value = "/api/webapi/userService")
@Api(value = "/userService", tags = "用户管理-用户服务")
@RestController
public class UserController extends BaseApiController {

	private IPartyUserService userService;

	public UserController() {
		userService = AppUtil.getBean(IPartyUserService.class);
	}

	@ApiOperation(value = "获取用户信息", notes = "获取当前用户信息")
	@RequestMapping(value = "/loadUser", method = RequestMethod.GET)
	public APIResult<User> loadUser() {
		APIResult<User> result = new APIResult<>();

		try {
			User user = userService.getById(ContextUtil.getCurrentUserId());
			result.setData(user);
			result.setMessage("获取用户信息成功！");
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取用户信息失败！", e);
		}

		return result;
	}

	@ApiOperation(value = "根据ID获取人员", notes = "根据ID获取人员")
	@RequestMapping(value = "/loadById", method = RequestMethod.GET)
	public APIResult<User> loadById(
			@ApiParam(value = "用户ID", required = true) @RequestParam(name = "id", required = true) String id) {
		APIResult<User> result = new APIResult<>();
		try {
			User user = userService.getById(id);
			result.setData(user);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取用户信息失败！", e);
		}

		return result;
	}

	@ApiOperation(value = "根据账号获取人员", notes = "根据账号获取人员")
	@RequestMapping(value = "/loadByAccount", method = RequestMethod.GET)
	public APIResult<User> loadByAccount(
			@ApiParam(value = "用户账号", required = true) @RequestParam(name = "account", required = true) String account) {
		User user = userService.getByAccount(account);

		APIResult<User> result = new APIResult<>();
		result.setData(user);

		return result;
	}

	@ApiOperation(value = "根据微信账号获取人员", notes = "根据微信账号获取人员")
	@RequestMapping(value = "/loadByWcAccount", method = RequestMethod.GET)
	public APIResult<User> loadByWcAccount(
			@ApiParam(value = "用户微信号", required = true) @RequestParam(name = "account", required = true) String account) {
		User user = userService.getByWcAccount(account);

		APIResult<User> result = new APIResult<>();
		result.setData(user);

		return result;
	}

	@ApiOperation(value = "是否管理员账号", notes = "根据账号判断是否管理员账号")
	@RequestMapping(value = "/isSuper", method = RequestMethod.GET)
	public APIResult<Map<String, Object>> isSuper(
			@ApiParam(value = "用户账号", required = true) @RequestParam(name = "account", required = true) String account) {
		APIResult<Map<String, Object>> result = new APIResult<>();
		try {
			boolean isSuper = userService.isSuperUser(account);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("isSuper", isSuper);
			
			result.setData(map);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取信息失败！", e);
		}

		return result;
	}

	@ApiOperation(value = "根据用户关系获取人员列表", notes = "根据用户关系获取人员列表")
	@RequestMapping(value = "/findByUserRelation", method = RequestMethod.GET)
	public APIResult<List<User>> findByUserRelation(
			@ApiParam(value = "用户账号", required = true) @RequestParam(name = "account", required = true) String account,
			@ApiParam(value = "用户参与者关系类型", required = true) @RequestParam(name = "partyRelType", required = true) String partyRelType,
			@ApiParam(value = "当前关系方1名称", required = true) @RequestParam(name = "relPartyName", required = true) String relPartyName,
			@ApiParam(value = "关系层次", required = false, defaultValue = "1") @RequestParam(name = "relLevel", required = true) int relLevel,
			@ApiParam(value = "递归查找", required = false, defaultValue = "false") @RequestParam(name = "isRecursion", required = true) boolean isRecursion) {
		APIResult<List<User>> result = new APIResult<>(); // TODO 参数不确定
		try {
			List<User> userList = userService.findByUserRelation(account, partyRelType, relPartyName, relLevel,
					isRecursion);
			result.setData(userList);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取人员列表失败！", e);
		}

		return result;
	}

	@ApiOperation(value = "根据用户关系获取人员列表", notes = "根据用户关系获取人员列表")
	@RequestMapping(value = "/findByUserRelLevel", method = RequestMethod.GET)
	public APIResult<List<User>> findByUserRelation(
			@ApiParam(value = "用户账号", required = true) @RequestParam(name = "account", required = true) String account,
			@ApiParam(value = "用户参与者关系类型", required = true) @RequestParam(name = "partyRelType", required = true) String partyRelType,
			@ApiParam(value = "当前关系方1名称", required = true) @RequestParam(name = "relPartyName", required = true) String relPartyName) {
		APIResult<List<User>> result = new APIResult<>();
		try {
			List<User> userList = userService.findByUserRelation(account, partyRelType, relPartyName);
			result.setData(userList);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取人员列表失败！", e);
		}

		return result;
	}

	@ApiOperation(value = "根据用户ID获取人员上下级负责人列表", notes = "根据用户ID获取人员上下级负责人列表")
	@RequestMapping(value = "/findSupOrSubByUserId", method = RequestMethod.GET)
	public APIResult<List<User>> findSupOrSubByUserId(
			@ApiParam(value = "用户ID", required = true) @RequestParam(name = "userId", required = true) String userId,
			@ApiParam(value = "关系层次", required = false, defaultValue = "1") @RequestParam(name = "relLevel", required = true) int relLevel,
			@ApiParam(value = "查找模式", required = true, defaultValue = "equal") @RequestParam(name = "pattern", required = true) String pattern) {
		APIResult<List<User>> result = new APIResult<>();
		try {
			List<User> userList = userService.findSupOrSubByUserId(userId, relLevel, pattern);
			result.setData(userList);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取人员上下级负责人列表失败！", e);
		}

		return result;
	}

	@ApiOperation(value = "根据用户账号获取人员上下级负责人列表", notes = "根据用户账号获取人员上下级负责人列表")
	@RequestMapping(value = "/findSupOrSubByAccount", method = RequestMethod.GET)
	public APIResult<List<User>> findSupOrSubByAccount(
			@ApiParam(value = "用户账号", required = true) @RequestParam(name = "account", required = true) String account,
			@ApiParam(value = "关系层次", required = false, defaultValue = "1") @RequestParam(name = "relLevel", required = true) int relLevel,
			@ApiParam(value = "查找模式", required = true, defaultValue = "equal") @RequestParam(name = "pattern", required = true) String pattern) {
		
		APIResult<List<User>> result = new APIResult<>();
		try {
			List<User> userList = userService.findSupOrSubByAccount(account, relLevel, pattern);
			result.setData(userList);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取人员上下级负责人列表失败！", e);
		}

		return result;
	}

	@ApiOperation(value = "根据用户ID获取人员上下级列表", notes = "根据用户ID获取人员上下级列表")
	@RequestMapping(value = "/findSupOrSubUserByUserId", method = RequestMethod.GET)
	public APIResult<List<User>> findSupOrSubUserByUserId(
			@ApiParam(value = "用户ID", required = true) @RequestParam(name = "userId", required = true) String userId,
			@ApiParam(value = "关系层次", required = false, defaultValue = "1") @RequestParam(name = "relLevel", required = true) int relLevel,
			@ApiParam(value = "查找模式", required = true, defaultValue = "equal") @RequestParam(name = "pattern", required = true) String pattern) {
		APIResult<List<User>> result = new APIResult<>();
		try {
			List<User> userList = userService.findSupOrSubUserByUserId(userId, relLevel, pattern);
			result.setData(userList);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取人员上下级列表失败！", e);
		}

		return result;
	}

	@ApiOperation(value = "根据用户账号获取人员上下级列表", notes = "根据用户账号获取人员上下级列表")
	@RequestMapping(value = "/findSupOrSubUserByAccount", method = RequestMethod.GET)
	public APIResult<List<User>> findSupOrSubUserByAccount(
			@ApiParam(value = "用户账号", required = true) @RequestParam(name = "account", required = true) String account,
			@ApiParam(value = "关系层次", required = false, defaultValue = "1") @RequestParam(name = "relLevel", required = true) int relLevel,
			@ApiParam(value = "查找模式", required = true, defaultValue = "equal") @RequestParam(name = "pattern", required = true) String pattern) {
		APIResult<List<User>> result = new APIResult<>();
		try {
			List<User> userList = userService.findSupOrSubUserByAccount(account, relLevel, pattern);
			result.setData(userList);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取人员上下级列表失败！", e);
		}

		return result;
	}

	@ApiOperation(value = "根据用户ID和关系获取人员列表", notes = "根据用户ID和关系获取人员列表")
	@RequestMapping(value = "/findByUserIdRelation", method = RequestMethod.GET)
	public APIResult<List<User>> findByUserIdRelation(
			@ApiParam(value = "用户ID", required = true) @RequestParam(name = "userId", required = true) String userId,
			@ApiParam(value = "用户参与者关系类型", required = true) @RequestParam(name = "partyRelType", required = true) String partyRelType,
			@ApiParam(value = "当前关系方1名称", required = true) @RequestParam(name = "relPartyName", required = true) String relPartyName) {
		
		APIResult<List<User>> result = new APIResult<>();
		try {
			List<User> userList = userService.findByUserIdRelation(userId, partyRelType, relPartyName);
			result.setData(userList);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取人员列表失败！", e);
		}

		return result;
	}

	@ApiOperation(value = "根据用户参与者关系获取人员列表", notes = "根据用户参与者关系获取人员列表")
	@RequestMapping(value = "/findByPartyRel", method = RequestMethod.GET)
	public APIResult<List<User>> findByPartyRel(
			@ApiParam(value = "参与者别名", required = true) @RequestParam(name = "alias", required = true) String alias,
			@ApiParam(value = "参与者类型", required = true) @RequestParam(name = "partyType", required = true) String partyType,
			@ApiParam(value = "用户参与者关系类型", required = true) @RequestParam(name = "partyRelType", required = true) String partyRelType) {
		APIResult<List<User>> result = new APIResult<>();
		try {
			List<User> userList = userService.findByPartyRelation(alias, partyType, partyRelType);
			result.setData(userList);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取人员列表失败！", e);
		}

		return result;
	}

	@ApiOperation(value = "根据用户组织别名获取组织的人员列表", notes = "根据用户组织别名获取组织的人员列表")
	@RequestMapping(value = "/findByPartyRelType", method = RequestMethod.GET)
	public APIResult<List<User>> findByPartyRelType(
			@ApiParam(value = "参与者别名", required = true) @RequestParam(name = "alias", required = true) String alias,
			@ApiParam(value = "参与者类型", required = true) @RequestParam(name = "partyType", required = true) String partyType) {
		APIResult<List<User>> result = new APIResult<>();
		try {
			List<User> userList = userService.findByPartyRelation(alias, partyType);
			result.setData(userList);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取组织的人员列表失败！", e);
		}

		return result;
	}

	@ApiOperation(value = "获取人员列表", notes = "根据用户扩展属性获取人员列表")
	@RequestMapping(value = "/findByAttrValue", method = RequestMethod.GET)
	public APIResult<List<User>> findByAttrValue(
			@ApiParam(value = "扩展属性key", required = true) @RequestParam(name = "key", required = true) String key,
			@ApiParam(value = "扩展属性值", required = true) @RequestParam(name = "value", required = true) String value) {
		
		APIResult<List<User>> result = new APIResult<>();
		try {
			List<User> userList = userService.findByAttrValue(key, value);
			result.setData(userList);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取人员列表失败！", e);
		}

		return result;
	}

	@ApiOperation(value = "根据扩展属性的参数 jsonConfig 获取人员列表", notes = "根据扩展属性的参数 jsonConfig 获取人员列表")
	@RequestMapping(value = "/findByPartyAttrParam", method = RequestMethod.GET)
	public APIResult<List<User>> findByPartyAttrParam(
			@ApiParam(value = "用户自定义参数", required = true) @RequestParam(name = "jsonConfig", required = true) String jsonConfig) {
		APIResult<List<User>> result = new APIResult<>();
		try {
			List<User> userList = userService.findByPartyAttrParam(jsonConfig);
			result.setData(userList);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取人员列表失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据扩展属性的参数 jsonConfig 获取人员列表", notes = "根据扩展属性的参数 jsonConfig 获取人员列表")
	@RequestMapping(value = "/findByUserAttrParam", method = RequestMethod.GET)
	public APIResult<List<User>> findByUserAttrParam(
			@ApiParam(value = "用户自定义参数", required = true) @RequestParam(name = "jsonConfig", required = true) String jsonConfig) {
		APIResult<List<User>> result = new APIResult<>();
		try {
			List<User> userList = userService.findByUserAttrParam(jsonConfig);
			result.setData(userList);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取人员列表失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据参与者ID获取下属人员列表", notes = "根据参与者ID获取下属人员列表")
	@RequestMapping(value = "/findByParty", method = RequestMethod.GET)
	public APIResult<List<User>> findByParty(
			@ApiParam(value = "参与者ID", required = true) @RequestParam(name = "partyId", required = true) String partyId) {
		APIResult<List<User>> result = new APIResult<>();
		try {
			List<User> userList = userService.findByParty(partyId);
			result.setData(userList);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取人员列表失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据参与者ID及类型获取下属人员列表", notes = "根据参与者ID及类型获取下属人员列表")
	@RequestMapping(value = "/findByPartyAndType", method = RequestMethod.GET)
	public APIResult<List<DefaultPartyUserPo>> findByPartyAndType(
			@ApiParam(value = "参与者ID", required = true) @RequestParam(name = "partyId", required = true) String partyId,
			@ApiParam(value = "参与者类型", required = true) @RequestParam(name = "partyType", required = true) String partyType,
			@ApiParam(value = "参与者姓名", required = false) @RequestParam(name = "name", required = false) String name) {
		APIResult<List<DefaultPartyUserPo>> result = new APIResult<>();
		try {
			Map<String, Object> params = b().a("partyType", partyType).a("id", "%" + partyId + "%").p();
			if (PartyType.ORG.getValue().equals(partyType) && StringUtil.isNotEmpty(partyId)) {
				params.put("id", partyId);
			} else if (PartyType.ORG.getValue().equals(partyType) && StringUtil.isEmpty(partyId)) {
				params.put("id", null);
			}
			if (StringUtil.isNotEmpty(name)) {
				params.put("name", "%" + name + "%");
				params.put("id", null);
			}
			String listData = userService.findByPartyJson(params);
			List<DefaultPartyUserPo> list = null;
			if (JacksonUtil.isJsonArray(listData)) {
				list = DefaultPartyUserPo.fromJsonArrayString2(listData);
			}
			result.setData(list);
			result.setMessage("获取人员信息成功！");
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取下属人员列表失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据角色ID获取下属人员列表", notes = "根据角色ID获取下属人员列表")
	@RequestMapping(value = "/findByRoleId", method = RequestMethod.GET)
	public APIResult<List<User>> findByRoleId(
			@ApiParam(value = "角色ID", required = true) @RequestParam(name = "roleId", required = true) String roleId) {
		APIResult<List<User>> result = new APIResult<>();
		try {
			List<User> userList = userService.findByRole(roleId);
			result.setData(userList);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取下属人员列表失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据角色别名获取下属人员列表", notes = "根据角色别名获取下属人员列表")
	@RequestMapping(value = "/findByRoleAlias", method = RequestMethod.GET)
	public APIResult<List<User>> findByRoleAlias(
			@ApiParam(value = "角色别名", required = true) @RequestParam(name = "roleAlias", required = true) String roleAlias) {
		APIResult<List<User>> result = new APIResult<>();
		try {
			List<User> userList = userService.findByRoleAlias(roleAlias);
			result.setData(userList);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取下属人员列表失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "获取所有人员列表", notes = "获取所有人员列表")
	@RequestMapping(value = "/findAll", method = RequestMethod.GET)
	public APIResult<List<User>> findAll() {
		APIResult<List<User>> result = new APIResult<>();
		try {
			List<User> userList = userService.findAll();
			result.setData(userList);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取所有人员列表失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据用户组ID获取人员列表", notes = "根据用户组ID获取人员列表")
	@RequestMapping(value = "/findByGroupId", method = RequestMethod.GET)
	public APIResult<List<User>> findByGroupId(
			@ApiParam(value = "用户组ID", required = true) @RequestParam(name = "groupId", required = true) String groupId) {
		APIResult<List<User>> result = new APIResult<>();
		try {
			List<User> userList = userService.findByGroupId(groupId);
			result.setData(userList);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取人员列表失败！", e);
		}
		return result;
	}

}
